<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="日志id" prop="logId">
        <el-input v-model="queryParams.logId" placeholder="请输入日志id" clearable style="width: 240px;" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="执行结果" prop="executeResultCode">

        <el-select v-model="queryParams.executeResultCode" placeholder="请选择执行结果" clearable size="small" style="width: 240px">
          <el-option key="" label="全部" value="" />
          <el-option key="1" label="成功" value="1" />
          <el-option key="2" label="失败" value="2" />
          <el-option key="3" label="异常" value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="操作时间">
        <el-date-picker v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['data:logs:export']">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="tables" v-loading="loading" :data="list" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志编号" prop="logId" width="300" show-overflow-tooltip />
      <el-table-column label="库/表名" prop="schemaTable" width="300" show-overflow-tooltip />

      <el-table-column label="执行类型" prop="operTypeCode">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.operTypeCode==1">{{scope.row.operTypeName}}</el-tag>
          <el-tag v-if="scope.row.operTypeCode==2" type="success">{{scope.row.operTypeName}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="同步结果" prop="executeResultCode">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.executeResultCode==1">{{scope.row.executeResultName}}</el-tag>
          <el-tag v-if="scope.row.executeResultCode==2" type="success">{{scope.row.executeResultName}}</el-tag>
          <el-tag v-if="scope.row.executeResultCode==3" type="danger">{{scope.row.executeResultName}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作时间" prop="createTime" width="150" show-overflow-tooltip />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row,scope.index)" v-hasPermi="['monitor:operlog:query']">详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="24">
            <el-form-item label="执行语句：">{{ form.executeSentence }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="open = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import loginVue from "../../login.vue";

import { listLog, exportListLog } from "@/api/areaSystem/synchronizationLog";

export default {
  name: "synchronizationLog",
  dicts: ['sys_oper_type', 'sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: { prop: 'operTime', order: 'descending' },
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        params: {
          //开始时间 
          beginTime: '',
          // 结束时间 
          endTime: '',
        },
        //执行结果
        executeResultCode: '',
        //日志id
        logId: '',
        createTime: '',
        updateTime: '',
        pageNum: 1,
        pageSize: 10

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询同步日志 */
    getList() {
      console.log("dateRange", this.dateRange)
      if (this.dateRange.length == 2) {
        //开始时间 
        this.queryParams.params.beginTime = this.dateRange[0]
        // 结束时间 
        this.queryParams.params.endTime = this.dateRange[1]

      } else {
        //开始时间 
        this.queryParams.params.beginTime = ''
        // 结束时间 
        this.queryParams.params.endTime = ''
      }



      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 操作日志类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.dict.type.sys_oper_type, row.businessType);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.operId)
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const operIds = row.operId || this.ids;
      this.$modal.confirm('是否确认删除日志编号为"' + operIds + '"的数据项？').then(function () {
        return delOperlog(operIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('是否确认清空所有操作日志数据项？').then(function () {
        return cleanOperlog();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("清空成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有操作日志数据项？').then(() => {
        this.exportLoading = true;
        return exportListLog(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => { });
    }
  }
};
</script>

